package huhx.linux.com.kotlinlearn.utils

import android.util.Log
import huhx.linux.com.kotlinlearn.constants.ConfigConstants
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitUtils {
    private val LOG_TAG = RetrofitUtils::class.java.simpleName

    private var retrofit: Retrofit? = null

    fun getRetrofit(): Retrofit? {
        Log.i(LOG_TAG, "retrofit = " + retrofit)
        if (retrofit == null) {
            val client = OkHttpClient().newBuilder()
                    .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                    .build()
            retrofit = Retrofit.Builder()
                    .baseUrl(ConfigConstants.BASE_URL)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build()
        }
        return retrofit
    }
}